package com.zhen.aikan.util;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.ListUtil;
import cn.hutool.json.JSONUtil;
import com.zhen.aikan.entity.VodDetail;
import com.zhen.aikan.entity.crawl.VodApi;
import com.zhen.aikan.entity.crawl.VodDataList;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.client.ResourceAccessException;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.List;

/**
 * @author LiJiaZhen
 * @date 2023/9/16 13:41
 */
@Slf4j
@Component
public class Spider {
    @Autowired
    private RestTemplate restTemplate;

    private static RestTemplate staticRestTemplate;

    protected static final Logger logger = LoggerFactory.getLogger(Spider.class);

    /**
     * 解决静态自动注入问题
     */
    @PostConstruct
    public void init() {
        staticRestTemplate = restTemplate;
    }



    public static List<VodDetail> crawlVideo(String url) {
        String jsonStr = staticRestTemplate.getForObject(url, String.class);
        VodApi vodApi = JSONUtil.toBean(jsonStr, VodApi.class);
        List<VodDataList> vodList = vodApi.getList();
        List<VodDetail> vodDetails = new ArrayList<>();
        if(vodList.isEmpty()){
            return null;
        }
        vodList.forEach(item -> {
            VodDetail vodDetail = new VodDetail();
            vodDetail.setVodId(item.getVod_id());
            vodDetail.setTypeId(item.getType_id());
            vodDetail.setTypePid(item.getType_id_1());
            vodDetail.setVodPlayUrl(item.getVod_play_url());
            vodDetail.setVodClass(item.getVod_class());
            vodDetail.setVodActor(item.getVod_actor());
            vodDetail.setVodArea(item.getVod_area());
            vodDetail.setVodBlurb(item.getVod_blurb());
            vodDetail.setVodContent(item.getVod_content());
            vodDetail.setVodDirector(item.getVod_director());
            vodDetail.setVodDoubanScore(item.getVod_douban_score());
            vodDetail.setVodDown(item.getVod_down());
            vodDetail.setVodEn(item.getVod_en());
            vodDetail.setVodHits(item.getVod_hits());
            vodDetail.setVodHitsDay(item.getVod_hits_day());
            vodDetail.setVodHitsMonth(item.getVod_hits_month());
            vodDetail.setVodHitsWeek(item.getVod_hits_week());
            vodDetail.setVodLang(item.getVod_lang());
            vodDetail.setVodYear(item.getVod_year());
            vodDetail.setVodWriter(item.getVod_writer());
            vodDetail.setVodTotal(item.getVod_total());
            vodDetail.setVodTime(item.getVod_time());
            vodDetail.setVodTag(item.getVod_tag());
            vodDetail.setVodSub(item.getVod_sub());
            vodDetail.setVodStatus(item.getVod_status());
            vodDetail.setVodSerial(item.getVod_serial());
            vodDetail.setVodScoreNum(item.getVod_score_num());
            vodDetail.setVodScoreAll(item.getVod_score_all());
            vodDetail.setVodRemarks(item.getVod_remarks());
            vodDetail.setVodPlayFrom(item.getVod_play_from());
            vodDetail.setVodPic(item.getVod_pic());
            vodDetail.setVodName(item.getVod_name());
            vodDetail.setVodLetter(item.getVod_letter());
            vodDetail.setVodScore(item.getVod_score());
            vodDetail.setVodVersion(item.getVod_version());

            logger.info("视频id为：{}；名字为：{}；更新时间为：{}；更新的状态：{}",
                    item.getVod_id(),
                    item.getVod_name(),
                    item.getVod_time(),
                    item.getVod_remarks());

            vodDetails.add(vodDetail);
        });
        return vodDetails;
    }
}
